package org.gnyxy.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class ExcelUtils {
    /**
     * 转为CSV的字符串
     * @param list
     * @return
     */
    private static String toCsv(List<Map<Integer, String>> list){

        if (CollUtil.isEmpty(list)) {
            return "";
        }

        StringBuilder builder = new StringBuilder();
        // 获取表头
        LinkedHashMap<Integer, String> title =(LinkedHashMap<Integer, String>)list.get(0);
        List<String> titleValue = title.values().stream().filter(ObjectUtil::isNotEmpty)
                .collect(Collectors.toList());

        builder.append(StringUtils.join(titleValue,",")).append("\n");

        for (int i = 1; i < list.size(); i++){
            LinkedHashMap<Integer, String> table =(LinkedHashMap<Integer, String>) list.get(i);
            List<String> tableValue = table.values().stream().filter(ObjectUtil::isNotEmpty)
                    .collect(Collectors.toList());

            builder.append(StringUtils.join(tableValue,",")).append("\n");
        }

        return builder.toString();
    }
    public static String xlsxToCsv(MultipartFile multipartFile) {
        List<Map<Integer, String>> list = null;
        try {
            list = EasyExcel.read(multipartFile.getInputStream())
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet()
                    .headRowNumber(0)
                    .doReadSync();
        } catch (Exception e) {
            log.error("表格处理错误", e);
            throw new RuntimeException("表格处理错误", e);
        }

        return toCsv(list);
    }

    public static String xlsToCsv(MultipartFile multipartFile) {
        List<Map<Integer, String>> list = null;
        try {
            list = EasyExcel.read(multipartFile.getInputStream())
                    .excelType(ExcelTypeEnum.XLS)
                    .sheet()
                    .headRowNumber(0)
                    .doReadSync();
        } catch (Exception e) {
            log.error("表格处理错误", e);
            throw new RuntimeException("表格处理错误", e);
        }

        return toCsv(list);
    }

    public static String csvToString(MultipartFile multipartFile) {
        List<Map<Integer, String>> list = null;
        try {
            list = EasyExcel.read(multipartFile.getInputStream())
                    .excelType(ExcelTypeEnum.CSV)
                    .sheet()
                    .headRowNumber(0)
                    .doReadSync();
        } catch (Exception e) {
            log.error("表格处理错误", e);
            throw new RuntimeException("表格处理错误", e);
        }

        return toCsv(list);
    }


}